<template>
  <ex-dialog width="600px" class="ex-diolog" :title="title" :visible="visible" :close-on-click-modal="false"
    @closeDialog="handleClose">
    <div class="main" v-loading="loading">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" :label-width="labelWidth" class="demo-ruleForm">
        <el-form-item label="随机抽取" prop="num">
          <el-input v-model.number="ruleForm.num" placeholder="请输入" :style="{width: inputWidth}">
          </el-input>
          <span style="margin-left: 10px">名</span>
        </el-form-item>
        <el-form-item label="获奖金数量" prop="amount">
          <el-input v-model="ruleForm.amount" placeholder="请输入" :style="{width: inputWidth}"></el-input>
          <span style="margin-left: 10px">{{awardAssetTypeDict}}</span>
        </el-form-item>
      </el-form>
    </div>
    <div class="button_box">
      <el-button type="primary" :loading="AgreeLoading" @click="submitForm('ruleForm')">确认添加</el-button>
      <el-button @click="handleClose">取消</el-button>
    </div>
  </ex-dialog>
</template>
<script>
  import Loading from "@/components/Loading/index";
  // api
  import {
    addJackpotAwardRandom,
  } from "@/api/consumeMiningManagement/onlineClockingActivity";

  export default {
    components: {
      Loading,
    },
    props: {
      visible: Boolean,
      id: String,
      awardAssetType: String
    },
    data() {
      return {
        labelWidth: '120px',
        inputWidth: '300px',
        title: '添加随机奖励',
        loading: false,
        disabled: false,
        awardAssetTypeDict: '', //奖池资产类型名称
        AgreeLoading: false, //按钮loading
        ruleForm: {
          amount: '', //奖励数量
          num: '', //奖励人数
          punchActivityId: '', //打卡活动id
        },
        rules: {
          num: [{
            required: true,
            message: '请输入',
            trigger: ['change', 'bulr'],
          }],
          amount: [{
            required: true,
            message: "请输入获奖金数量",
            trigger: ['change', 'bulr'],
          }],
        },
      };
    },
    computed: {},
    created() {
        this.ruleForm.punchActivityId = this.id
        if(this.awardAssetType==1){
          this.awardAssetTypeDict = '椰分'
        }else if(this.awardAssetType==2){
          this.awardAssetTypeDict = 'CoCo'
        }else{
          this.awardAssetTypeDict = '现金'
        }
    },
    methods: {
      //取消
      handleClose(formName) {
        this.$emit("update:visible", false);
      },
      submitForm(formName) {
        //创建确定
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.AgreeLoading = true
            var params = {
              ...this.ruleForm,
            };
            addJackpotAwardRandom(params).then((res) => {
                this.$message({
                  message: res.result,
                  type: "success",
                });
                setTimeout(() => {
                  this.$emit('success')
                  this.handleClose()
                  this.AgreeLoading = false
                }, 1000);
              })
              .catch(() => {
                this.AgreeLoading = false;
              });

          } else {
            console.log("error submit!!");
            return false;
          }
        });
      },
    },
  };

</script>
<style lang="scss" scoped>
  .ex-diolog {
    ::v-deep .ex--dialog--footer {
      border: none !important;
      padding: 0 !important;
    }

    ::v-deep .ex--dialog--main {
      padding: 10px 20px !important;
    }
  }

  .main {
    padding: 20px 20px;
  }

  .button_box {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px 0;
  }

</style>
